package org.example.controller;

import org.example.config.PropertiesConfig;
import org.example.param.CategoryAddParam;
import org.example.param.CategoryFuzzyParam;
import org.example.param.CategoryUpdateParam;
import org.example.service.CategoryService;
import org.example.utils.FileUploadUtil;
import org.example.utils.HttpCode;
import org.example.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @GetMapping("/all")
    @PreAuthorize("permitAll()")
    public Result all(){
        return categoryService.all();
    }

    @PostMapping("/add")
    @PreAuthorize("hasAnyRole('admin')")
    public Result add(@RequestBody @Validated CategoryAddParam categoryAddParam){
        return categoryService.add(categoryAddParam);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAnyRole('admin')")
    public Result update(@RequestBody @Validated CategoryUpdateParam categoryUpdateParam){
        return categoryService.update(categoryUpdateParam);
    }

    @PostMapping("/delete")
    @PreAuthorize("hasAnyRole('admin')")
    public Result delete(@RequestBody Map<String,Long> map){
        Long id = map.get("id");
        if (id == null){
            return Result.fail(HttpCode.PARAM_ERROR);
        }
        return categoryService.delete(id);
    }


    /**
     * 根据名称查找类别
     */
    @PostMapping("/findCategoryByName")
    @PreAuthorize("permitAll()")
    public Result findCategoryByName(@RequestBody Map<String,String> map){
        String categoryName = map.get("categoryName");
        return categoryService.findCategoryByName(categoryName);
    }

    /**
     * 根据名称查找类别
     */
    @PostMapping("/findCategoryById")
    @PreAuthorize("permitAll()")
    public Result findCategoryById(@RequestBody Map<String,String> map){
        Long id = Long.valueOf(map.get("id"));
        return categoryService.findCategoryById(id);
    }

    @PostMapping("fuzzy")
    @PreAuthorize("permitAll()")
    public Result fuzzy(@RequestBody CategoryFuzzyParam categoryFuzzyParam){
        return categoryService.fuzzy(categoryFuzzyParam);
    }

    @PostMapping("hot")
    @PreAuthorize("permitAll()")
    public Result hot(@RequestBody(required = false) Map<String,Integer> map){
        if (map == null) map = new HashMap<>();
        Integer limit = map.get("limit");
        return categoryService.hot(limit);
    }

    /**
     * 上传图片
     */
    @PostMapping("/uploadAvatar")
    @PreAuthorize("hasAnyRole('admin')")
    public Result uploadAvatar(@RequestParam("image")MultipartFile file) throws IOException {
        String url = FileUploadUtil.upload(PropertiesConfig.getUploadCategoryDir(), file, FileUploadUtil.IMAGE_ALLOWED_EXTENSION);
        return Result.success(FileUploadUtil.transformUrl(url));
    }


}
